<?php

namespace app\home\service;

use app\common\helpers\PayHelper;
use app\home\model\Order;
use app\home\model\UserOrder;
use think\Config;

class Jingduyouxi implements IPay
{
    private $config = null;
    private $helper = null;

    public function __construct($config)
    {
        $this->config = $config;
        $this->helper = PayHelper::getInstance($this->config['channel']);
    }

    public function createOrder($params)
    {
        return
        // 计费点映射关系
        $map_class = Config::get('MapPayClass');
        if(!isset($map_class[$params['channel']])){
            $this->helper->record('channel map not found');
            return false;
        }

        $orderModel = new UserOrder();
        $data = $orderModel->create_order_old($params['userid'], $params['channel'], $params['stroeid'], 5,null);

        $info = json_decode($data, true);
        $return['orderid'] = $info['orderid'];
        $return['product_id'] = $params['stroeid'];
        $return['product_price'] = $info['price'];
        $return['product_name'] = $info['subject'];

        $data_Arr['code'] = 'success';
        $data_Arr['data'] = $return;

        return $data_Arr;
    }
    public function processNotify($params)
    {
        $split_str = str_repeat('-',80);
        $this->helper->record($split_str);
        $this->helper->record($params);

        if(empty($params['tradeNo']) || empty($params['orderNo']) || empty($params['pid'])){
            $this->helper->record('some params empty');
            return 'FAIL';
        }

        $order_info = (new Order)->where('id', $params['tradeNo'])->find();

        if (empty($order_info)) {
            $this->helper->record('order empty');
            return 'FAIL';
        }

        if ($order_info['status'] == 1) {
            $this->helper->record('order status error');
            return 'FAIL';
        }

        // 发货
        $time = date('Y-m-d H:i:s', time());
        $re = Db_connect_server_pay_domian($params['tradeNo'], $order_info['amount'], $time);
        if ($re == 'success') {
            return 'SUCCESS';
        }

        $this->helper->record('delivery fail');
        return 'FAIL';
    }
}
